package proteus.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import proteus.excepcion.DAOExcepcion;
import proteus.modelo.Caracteristica;
import proteus.modelo.Local;
import proteus.util.ConexionBD;


public class CaracteristicaDAO extends BaseDAO{
	
	public void insertar(Caracteristica vo) throws DAOExcepcion {
		System.out.println("CaracteristicaDAO: insertar(Caracteristica vo)");
		String query = "INSERT INTO caracteristica(noCaracteristica) VALUES (?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
//			stmt.setInt(1, vo.getCoCaracteristica());
			stmt.setString(1, vo.getNoCaracteristica());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

//	LISTAR CARACTERISTICAS
	public Collection<Caracteristica> listarCaracteristica() throws DAOExcepcion {
		System.out.println("CaracteristicaDAO: listarCaracteristicas()");
		Collection<Caracteristica> c = new ArrayList<Caracteristica>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {	        
			con = ConexionBD.obtenerConexion();
			String query="Select coCaracteristica, noCaracteristica from caracteristica order by noCaracteristica";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Caracteristica vo = new Caracteristica();
				vo.setCoCaracteristica(rs.getInt("coCaracteristica"));
				vo.setNoCaracteristica(rs.getString("noCaracteristica"));
				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	
	
}